<template>
    <div class="login_container">
        <!-- 登录模块盒子 -->
<div class="login-box">
<!-- 图片盒子 -->

<!-- 登陆表单区域 -->
<h3 class="text">申爱后台管理系统</h3>
<el-form  class="login_form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef" size="medium">
    <!-- :model数据绑定对象，绑定到login from这个表单中,rules表单的验证规则对象 -->
    <!-- 用户登录 -->
  <el-form-item prop="username">
    <el-input  prefix-icon="el-icon-user-solid" v-model="loginForm.username" size="mini" placeholder="请输入用户名"></el-input>
    <!-- v-model双向绑定，绑定到用户名 -->
  </el-form-item>
  <!-- 密码 -->
  <el-form-item prop="password">
    <el-input  prefix-icon="el-icon-lock" v-model="loginForm.password" type="password" size="mini" placeholder="请输入密码"></el-input>
     <!-- v-model双向绑定，绑定到用户登录密码,type="password可以使得密码隐藏"-->
  </el-form-item>
  <el-form-item prop="code">
    <el-input  prefix-icon="el-icon-s-opportunity" v-show="isRegister" v-model="loginForm.code" size="mini" placeholder="请输入邀请码"></el-input>
     <!-- v-model双向绑定，绑定到用户登录密码,type="password可以使得密码隐藏"-->
  </el-form-item>
  <!-- 按钮区域 -->
  <el-form-item class="btns">
    <el-button type="text" @click="isRegister=true;resetLoginForm()"  size="mini" v-show="!isRegister">去注册</el-button>
    <el-button type="text" @click="isRegister=false;resetLoginForm()"  size="mini" v-show="isRegister">返回</el-button>
    <el-button type="primary" @click="login" size="mini" v-show="!isRegister">登录</el-button>
    <el-button type="primary" @click="register" size="mini" v-show="isRegister">注册</el-button>
    <!-- 为登录绑定一个单击事件，名为login -->
    <el-button type="info" @click="resetLoginForm" size="mini">取消</el-button>
    <!-- click绑定单击事件，名为resetloginfrom -->
  </el-form-item>
  </el-form>
    </div>
</div>
</template>
 
<script>
import {Login,Register} from '@/api/login.js'
import { setToken } from '@/util/auth';
export default {
    name:"LogIn",
  data () {
    return {
        isRegister:false,
      loginForm: {
        username: '',
        password: '',
        code:''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    register(){
        this.$refs.loginFormRef.validate( valid => {
        if (!valid) {return this.$message.error('用户名密码不正确')}
        Register(this.loginForm).then( res => {
        console.log(res);

        this.$message.success('注册成功')
      
        this.isRegister=false;
        this.resetLoginForm()
      }).catch( error => {
        console.log(error);
      })
      })
    },
    login () {
      this.$refs.loginFormRef.validate( valid => {
        if (!valid) {return this.$message.error('用户名密码不正确')}
      Login(this.loginForm.username,this.loginForm.password).then( res => {
       
        this.$message.success('登陆成功')

        setToken(res.data.data.token)
        localStorage.setItem('shenai-userName', this.loginForm.username);
        this.$router.push('/uploadPage')  
      }).catch( error => {
        console.log(error);
      })
      })
    }
  }
}
</script>
 
<style lang="scss" scoped>
.login_container {
    background-color: #0e1729;
    background-image: url(@/assets/bg4.jpg);
    background-size: cover;
    height: 100vh;
    width: 100vw;
}
.login-box {
    width: 360px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%; 
    top: 50%; 
    transform: translate(-50%, -50%); 
}
 .avatar_box {
        height: 100px;
        width: 100px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #eee;
        position: absolute;
        left: 50%;
        top:-40%;
        transform: translate(-50%);
        background-color: rgb(32, 181, 201);
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
 
.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
    .btns {
        display: flex;
        justify-content: flex-end;
    }
    .text { 
      text-align: center;
      margin-top: 25px;
      font-size: 20px;
      font-family: 宋体;
    }
</style>